package com.sunday.android.fangdaifu.bukong.activity.index;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.entity.event.DoSearch;
import com.sunday.android.fangdaifu.bukong.entity.event.FinishOption;
import com.sunday.android.fangdaifu.bukong.fragment.index.OptinFragment;
import com.sunday.member.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
* 开始调试
* */
public class StartOptionActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    @Bind(R.id.viewPager)
    ViewPager viewPager;
    @Bind({R.id.tvName01, R.id.tvName02, R.id.tvName03})
    List<TextView> nameList;
    @Bind({R.id.point01, R.id.point02, R.id.point03})
    List<TextView> pointList;
    @Bind({R.id.tab01, R.id.tab02, R.id.tab03})
    List<LinearLayout> layoutList;
    @Bind(R.id.etSearch)
    EditText etSearch;
    @Bind(R.id.imgSearch)
    ImageView imgSearch;

    List<Fragment> fragmentList = new ArrayList<>();
    private long id;//point id

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_option);
        ButterKnife.bind(this);
        id = getIntent().getLongExtra("id",0);
        initView();
    }

    private void initView() {
        fragmentList.add(OptinFragment.instance(1,id));
        fragmentList.add(OptinFragment.instance(2,id));
        fragmentList.add(OptinFragment.instance(3,id));
        viewPager.setAdapter(new Adapter(getSupportFragmentManager()));
        viewPager.addOnPageChangeListener(this);
        viewPager.setOffscreenPageLimit(3);
        setSelectItem(0);
        etSearch.setOnKeyListener(onKeyListener);
    }

    private void setSelectItem(int position) {
        for (int i = 0; i < 3; i++) {
            nameList.get(i).setSelected(false);
            pointList.get(i).setBackgroundColor(Color.TRANSPARENT);
            nameList.get(i).setTypeface(Typeface.DEFAULT, Typeface.NORMAL);
            if (position == i) {
                nameList.get(i).setSelected(true);
                pointList.get(i).setBackgroundColor(Color.parseColor("#000000"));
                nameList.get(i).setTypeface(Typeface.DEFAULT_BOLD, Typeface.BOLD);
                viewPager.setCurrentItem(i, false);
            }
        }
    }

    @OnClick({R.id.tab01, R.id.tab02, R.id.tab03,R.id.imgSearch})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tab01:
                setSelectItem(0);
                break;
            case R.id.tab02:
                setSelectItem(1);
                break;
            case R.id.tab03:
                setSelectItem(2);
                break;
            case R.id.imgSearch:
                doSearch(etSearch.getText().toString().trim());
                break;
        }
    }


    private View.OnKeyListener onKeyListener = new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if(keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){
                doSearch(etSearch.getText().toString().trim());
                return true;
            }
            return false;
        }
    };

    private void doSearch(String num){
        OptinFragment optinFragment = (OptinFragment) fragmentList.get(viewPager.getCurrentItem());
        optinFragment.doSearch(new DoSearch(num));
        closeInput(etSearch);
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setSelectItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public void onEvent(FinishOption option){
        finish();
    }

    class Adapter extends FragmentPagerAdapter {

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }
}
